﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BO
{
    partial class DateTargets
    {
        public double _ReachedAmount;
        
        public double ReachedAmount
        {
            get { return _ReachedAmount; }
        }

        public string _Principal;
        
        public string Principal
        {
            get { return _Principal; }
        }

        public double _Rate;
        public string Rate
        {
            get
            {
                return Math.Round(_Rate, 2) + "%";

            }
        }
        public void LoadReachedAmount(BillingList billings,
            CompanyList companys,
            CenterList centers,
            DepartmentList departments
            )
        {
         
            double ret = 0;

            if (this.CompanyName == string.Empty)
            {
            }
            else if (CenterName == string.Empty && DepartmentName == string.Empty)
            {
                ret =
                    (from item in billings
                     where item.CompanyName == this.CompanyName
                     && item.TransDate >= BeginDate
                     && item.TransDate <= EndDate
                     select item.Amount).Sum();
                _Principal = (from item in companys
                              where item.Name == this.CompanyName
                              select item.Principal).FirstOrDefault();

            }
            else if (DepartmentName == string.Empty)
            {
                ret =
                   (from item in billings
                    where item.CompanyName == this.CompanyName
                    && item.CenterName == this.CenterName
                     && item.TransDate >= BeginDate
                      && item.TransDate <= EndDate
                    select item.Amount).Sum();
                _Principal = (from item in centers
                              where item.Name == this.CenterName
                                && item.CompanyName == this.CompanyName
                              select item.Principal).FirstOrDefault();
            }
            else
            {
                ret =
                  (from item in billings
                   where item.CompanyName == this.CompanyName
                   && this.CenterName.Contains(item.CenterName)
                    && item.DepartmentName == this.DepartmentName
                     && item.TransDate >= BeginDate
                    && item.TransDate <= EndDate
                   select item.Amount).Sum();
                _Principal = (from item in departments
                              where item.Name == this.DepartmentName
                              && item.CenterName == this.CenterName
                                && item.CompanyName == this.CompanyName
                              select item.Principal).FirstOrDefault();
            }

            _ReachedAmount = ret;
            if (this.Amount != 0)
            {
                _Rate = _ReachedAmount / this.Amount * 100;
            }

        }
    }
}
